package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务表 服务实现类
 * </p>
 *
 * @author zhangrui
 * @since 2024-11-03
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {
    @Resource
    private ServeItemMapper serveItemMapper;

    @Resource
    private RegionMapper regionMapper;

    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {

        PageResult<ServeResDTO> serveResDTOPageResult =  PageHelperUtils.selectPage(servePageQueryReqDTO,
                () -> baseMapper.queryServeListByRegionId(servePageQueryReqDTO.getRegionId()));
        return serveResDTOPageResult;

    }

    @Override
    @Transactional
    public void batchAdd(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        for(ServeUpsertReqDTO serveUpsertReqDTO : serveUpsertReqDTOList){
            //1.校验服务项是否为启用状态，不是启用状态不能新增
            ServeItem serveItem =  serveItemMapper.selectById(serveUpsertReqDTO.getServeItemId());
            if(ObjectUtil.isNull(serveItem)||serveItem.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
                throw new ForbiddenOperationException("该服务未启用无法添加到区域下使用");
            }
            //2.校验是否重复新增
            Integer count =  lambdaQuery().eq(Serve::getRegionId,serveUpsertReqDTO.getRegionId()).eq(Serve::getServeItemId,serveUpsertReqDTO.getServeItemId()).count();
            if(count>0){
                throw new ForbiddenOperationException(serveItem.getName()+"服务已存在");
            }
            //添加数据
            Serve serve = BeanUtil.toBean(serveUpsertReqDTO,Serve.class);
            Region region = regionMapper.selectById(serveUpsertReqDTO.getRegionId());
            serve.setCityCode(region.getCityCode());
            baseMapper.insert(serve);
        }

    }
    @Override
    @Transactional
    public Serve update(Long id, BigDecimal price) {
       boolean update = lambdaUpdate().eq(Serve::getId,id).set(Serve::getPrice,price).update();
        if(!update){
            throw new CommonException("修改服务价格失败");
        }
        Serve serve = baseMapper.selectById(id);
        return serve;
    }

    @Override
    @Transactional
    public Serve onSale(Long id){
        Serve serve = baseMapper.selectById(id);
        if(ObjectUtil.isNull(serve)){
            throw new ForbiddenOperationException("区域服务不存在");
        }
        //上架状态
        Integer saleStatus = serve.getSaleStatus();
        //草稿或下架状态方可上架
        if (!(saleStatus==FoundationStatusEnum.INIT.getStatus() || saleStatus==FoundationStatusEnum.DISABLE.getStatus())) {
            throw new ForbiddenOperationException("草稿或下架状态方可上架");
        }
        //服务项id
        Long serveItemId = serve.getServeItemId();
        ServeItem serveItem = serveItemMapper.selectById(serveItemId);
        if(ObjectUtil.isNull(serveItem)){
            throw new ForbiddenOperationException("所属服务项不存在");
        }
        //服务项的启用状态
        Integer activeStatus = serveItem.getActiveStatus();
        //服务项为启用状态方可上架
        if (!(FoundationStatusEnum.ENABLE.getStatus()==activeStatus)) {
            throw new ForbiddenOperationException("服务项为启用状态方可上架");
        }

        //更新上架状态
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .update();
        if(!update){
            throw new CommonException("启动服务失败");
        }
        return baseMapper.selectById(id);

    }

    @Override
    @Transactional
    public void delete(Long id) {
        Serve serve =  baseMapper.selectById(id);
        if(ObjectUtil.isNull(serve)){
            throw new CommonException("当前区域服务不存在，删除区域服务失败");
        }else{
            baseMapper.deleteById(id);
            System.out.println("删除区域服务id为："+serve.getId());
        }

    }

    @Override
    public Serve offSale(Long id) {
        //查询服务是否存在
        Serve serve = baseMapper.selectById(id);
        if(ObjectUtil.isNull(serve)){
            throw new CommonException("当前区域服务不存在，删除区域服务失败");
        }
        //查询服务是否已下架
        if(serve.getSaleStatus()==FoundationStatusEnum.INIT.getStatus()||serve.getSaleStatus()==FoundationStatusEnum.DISABLE.getStatus()){
            throw new CommonException("当前已经是下架状态");
        }
        //更新 下架服务
       boolean  update=  lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus,FoundationStatusEnum.DISABLE.getStatus())
                .update();
        if(!update){
            throw new CommonException("服务下架失败，请重试");
        }
        return baseMapper.selectById(id);
    }
}
